import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { setRouterAuth } from './router.auth'
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch((err) => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: '',
    component: HomeView,
    children: [
      {
        path: '/',
        redirect: 'users'
      },
      {
        path: '/home/users',
        name: 'users',
        component: () => import('../views/car-pages/Audi.vue')
      },
      {
        path: '/home/questions',
        name: 'bmw',
        component: () => import('../views/car-pages/Bmw.vue')
      },
      {
        path: '/home/interviewBlog',
        name: 'merc',
        component: () => import('../views/car-pages/Merc.vue')
      }
    ]
  },
  {
    path: '/',
    name: '',
    component: () => import('../views/login'),
    children: [
      {
        path: '/login',
        name: 'login',
        component: () => import('../views/login/components/Login.vue')
      },
      {
        path: '/register',
        name: 'register',
        component: () => import('../views/login/components/Register.vue')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach = setRouterAuth(router)

export default router
